package cn.kgc.mybatis.web.utils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author 课工场
 * @date 2024/10/31
 * @description  基础Servlet类 实现请求的分发   根据方法名实现方法调用
 *   method = "add"
 */
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 声明请求编码格式
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");

        //  反射   类对象
        try {
            Method declaredMethod = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.setAccessible(true);
            declaredMethod.invoke(this,req,resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
